package com.maple.testannotationprocessor;

import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

import cn.com.vipkid.annotationtest.InheritAnnotation;


@AutoService(Processor.class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes({"cn.com.vipkid.annotationtest.InheritAnnotation"})
public class TestAnnotationProcessor extends AbstractProcessor {

    /**
     * 文件相关的辅助类
     */
    private Filer mFiler;
    /**
     * 元素相关的辅助类
     */
    private Elements mElementUtils;
    /**
     * 日志相关的辅助类
     */
    private Messager mMessager;

    private int i=0;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        mElementUtils = processingEnv.getElementUtils();
        mMessager = processingEnv.getMessager();
        mFiler = processingEnv.getFiler();
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        System.out.println("TestAnnotationProcessor   开始处理注解");
        Iterator<Element> elements = (Iterator<Element>) roundEnvironment.getRootElements().iterator();
        while(elements.hasNext()){
            Element element = elements.next();
            System.out.println("TestAnnotationProcessor:"+element.toString());
            InheritAnnotation inheritAnnotation = element.getAnnotation(InheritAnnotation.class);
            if(inheritAnnotation == null)
                continue;
            System.out.println("TestAnnotationProcessor   开始生成文件");
            // 类名和包名
            TypeSpec finderClass = TypeSpec.classBuilder("MyGeneratedClass"+i)
                    .addModifiers(Modifier.PUBLIC)
//                                    .addSuperinterface(ParameterizedTypeName.get(TypeUtil.INJECTOR, TypeName.get(mClassElement.asType())))
//                                    .addMethod(methodBuilder.build())
                    .build();

            // 创建Java文件
            JavaFile javaFile = JavaFile.builder("com.maple.annotation.demo", finderClass).build();

            try {
                javaFile.writeTo(mFiler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            i++;
        }
        return true;
    }
}
